package com.kang.service.subject.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kang.Constants;
import com.kang.bean.bot.Answer;
import com.kang.bean.bot.Subject;
import com.kang.commons.util.HttpClientUtil;
import com.kang.mapper.AnswerMapper;
import com.kang.mapper.SubjectMapper;
import com.kang.service.subject.SubjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

/**
 * @program: service
 * @description:
 * @author: K.faWu
 * @create: 2022-04-29 14:20
 **/
@Service
public class SubjectServiceImpl extends ServiceImpl<SubjectMapper, Subject> implements SubjectService {
    @Autowired
    private SubjectMapper subjectMapper;
    @Autowired
    private AnswerMapper answerMapper;

    @Override
    public PageInfo<Subject> getList(Subject subject, Page<?> page) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        List<Subject> list = subjectMapper.selectList(new QueryWrapper<>());
        return new PageInfo<>(list);
    }

    @Override
    public Answer getAnswer(String text, String type) {
        List<Answer> answers = null;
        QueryWrapper<Answer> answerQueryWrapper = new QueryWrapper<>();
        if (text==null || "".equals(text)){
            answerQueryWrapper.eq("sub_id", -1);
            answers = answerMapper.selectList(answerQueryWrapper);

        }else{
            List<Subject> subjects = subjectMapper.getByTextAndType(text, type);
            if (subjects!= null && subjects.size()!= 0) {
                Subject subject = subjects.get(0);
                answerQueryWrapper.eq("sub_id", subject.getId());
                answers = answerMapper.selectList(answerQueryWrapper);
            }
        }

        Answer result = null;
        //是否有问题的答案
        if(answers != null && answers.size() !=0){
            Answer answer1 = answers.get(0);
            if (Constants.API.equals(answer1.getType())){
                String s = HttpClientUtil.doGet(answer1.getText());

                JSONObject object = JSONObject.parseObject(s);
                String imgurl = (String)object.get("imgurl");

                result = new Answer();
                result.setType(Constants.IMAGE);
                result.setText(imgurl);
                result.setWeight(1.0);
            }else {
                //根据答案的比重抽取答案
                BigDecimal sum = new BigDecimal("0");
                Map<BigDecimal, Answer> answerMap = new HashMap<>(8);
                List<BigDecimal> list = new ArrayList<>();
                for (Answer answer: answers) {
                    sum = sum.add(BigDecimal.valueOf(answer.getWeight()));
                    answerMap.put(sum, answer);
                    list.add(sum);
                }
                double random = Math.random();
                BigDecimal divide = new BigDecimal(random).multiply(sum);
                BigDecimal weight = new BigDecimal("0");
                for (BigDecimal bigDecimal: list){
                    if (divide.compareTo(bigDecimal)<0){
                        weight = bigDecimal;
                        break;
                    }
                }
                result = answerMap.get(weight);
            }
        }
        return result;
    }
}
